package com.group18.controller;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.group18.entity.FamilyUser;
import com.group18.service.AccountUserService;
import com.group18.service.FamilyUserService;
import com.group18.service.MemberUserService;


/**
 * Servlet implementation class DoDeleteMember
 */
@WebServlet("/DoDeleteMember")
public class DoDeleteMember extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DoDeleteMember() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		
		int member_id = Integer.parseInt(request.getParameter("id"));
		
		//删除家庭成员
		MemberUserService mus = new MemberUserService();
		boolean bl = mus.deleteByID(member_id);
		//删除家庭成员对应收支
		AccountUserService aus = new AccountUserService();
		aus.deleteAccountByMember_id(member_id);
		
		String msg ="";
		if(bl) {
			msg = "删除成功";
		}else {
			msg = "删除失败";
		}
		request.setAttribute("msg", msg);
		
		//获取日期
		Date d = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  
        String date = sdf.format(d); 
        request.setAttribute("date", date);
        
        
        String text = request.getParameter("text");
        HttpSession session = request.getSession();
        int id = (int)session.getAttribute("member_id");
        if(member_id == id) {//若删除的家庭成员是自身则返回登陆页面
        	//获得家庭列表
        	FamilyUserService fus = new FamilyUserService();
			List<FamilyUser> list = fus.selectAllUser();
			request.setAttribute("list", list);
			request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request, response);
        } else {//是否是经过搜索后的操作后的删除操作，若是则返回搜索后的页面
        	if(text != null && !text.equals("")) {
    			request.setAttribute("text", text);
    			request.getRequestDispatcher("/DoSearchMember").forward(request, response);
    		}else {
    			request.getRequestDispatcher("/ToFamilyMember").forward(request, response);
    		}
        }
        
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
